/*     */ package com.zimbra.cs.account.accesscontrol;
/*     */ 
/*     */ import com.zimbra.common.account.Key.DistributionListBy;
/*     */ import com.zimbra.common.account.Key.DomainBy;
/*     */ import com.zimbra.common.service.ServiceException;
/*     */ import com.zimbra.cs.account.Account;
/*     */ import com.zimbra.cs.account.AlwaysOnCluster;
/*     */ import com.zimbra.cs.account.CalendarResource;
/*     */ import com.zimbra.cs.account.Config;
/*     */ import com.zimbra.cs.account.Cos;
/*     */ import com.zimbra.cs.account.DistributionList;
/*     */ import com.zimbra.cs.account.Domain;
/*     */ import com.zimbra.cs.account.DynamicGroup;
/*     */ import com.zimbra.cs.account.Entry;
/*     */ import com.zimbra.cs.account.GlobalGrant;
/*     */ import com.zimbra.cs.account.Provisioning;
/*     */ import com.zimbra.cs.account.Provisioning.GroupMembership;
/*     */ import com.zimbra.cs.account.Server;
/*     */ import com.zimbra.cs.account.UCService;
/*     */ import com.zimbra.cs.account.XMPPComponent;
/*     */ import com.zimbra.cs.account.Zimlet;
/*     */ import com.zimbra.cs.ldap.LdapTODO.ACLTODO;
/*     */ import java.io.PrintStream;
/*     */ import java.util.ArrayList;
/*     */ import java.util.HashMap;
/*     */ import java.util.List;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ @LdapTODO.ACLTODO
/*     */ public abstract class TargetIterator
/*     */ {
/*     */   protected Provisioning mProv;
/*     */   protected TargetType mCurTargetType;
/*     */   protected Entry mTarget;
/*     */   protected boolean mCheckedSelf;
/*     */   protected boolean mNoMore;
/*     */   
/*     */   Entry next()
/*     */     throws ServiceException
/*     */   {
/*  56 */     if (this.mNoMore) {
/*  57 */       return null;
/*     */     }
/*  59 */     Entry grantedOn = null;
/*     */     
/*  61 */     if (!this.mCheckedSelf) {
/*  62 */       this.mCurTargetType = TargetType.global;
/*  63 */       this.mCheckedSelf = true;
/*  64 */       grantedOn = this.mTarget;
/*     */     }
/*  66 */     else if (this.mCurTargetType == TargetType.global) {
/*  67 */       this.mNoMore = true;
/*  68 */       grantedOn = this.mProv.getGlobalGrant();
/*     */     }
/*     */     
/*  71 */     return grantedOn;
/*     */   }
/*     */   
/*     */   private TargetIterator(Provisioning prov, TargetType initialTargetType, Entry target)
/*     */   {
/*  76 */     this.mProv = prov;
/*  77 */     this.mCurTargetType = initialTargetType;
/*  78 */     this.mTarget = target;
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   static TargetIterator getTargetIeterator(Provisioning prov, Entry target, boolean expandGroups)
/*     */     throws ServiceException
/*     */   {
/*  92 */     TargetIterator iter = null;
/*     */     
/*  94 */     if ((target instanceof CalendarResource)) {
/*  95 */       iter = new AccountTargetIterator(prov, target, expandGroups);
/*  96 */     } else if ((target instanceof Account)) {
/*  97 */       iter = new AccountTargetIterator(prov, target, expandGroups);
/*  98 */     } else if ((target instanceof Domain)) {
/*  99 */       iter = new DomainTargetIterator(prov, target);
/* 100 */     } else if ((target instanceof Cos)) {
/* 101 */       iter = new CosTargetIterator(prov, target);
/* 102 */     } else if ((target instanceof DistributionList)) {
/* 103 */       iter = new DistributionListTargetIterator(prov, target, expandGroups);
/* 104 */     } else if ((target instanceof DynamicGroup)) {
/* 105 */       iter = new DynamicGroupTargetIterator(prov, target);
/* 106 */     } else if ((target instanceof Server)) {
/* 107 */       iter = new ServerTargetIterator(prov, target);
/* 108 */     } else if ((target instanceof UCService)) {
/* 109 */       iter = new UCServiceTargetIterator(prov, target);
/* 110 */     } else if ((target instanceof Config)) {
/* 111 */       iter = new ConfigTargetIterator(prov, target);
/* 112 */     } else if ((target instanceof GlobalGrant)) {
/* 113 */       iter = new GlobalGrantTargetIterator(prov, target);
/* 114 */     } else if ((target instanceof Zimlet)) {
/* 115 */       iter = new ZimletTargetIterator(prov, target);
/* 116 */     } else if ((target instanceof XMPPComponent)) {
/* 117 */       iter = new XMPPComponentTargetIterator(prov, target);
/* 118 */     } else if ((target instanceof AlwaysOnCluster)) {
/* 119 */       iter = new AlwaysOnClusterTargetIterator(prov, target);
/*     */     } else {
/* 121 */       throw ServiceException.FAILURE("internal error - no TargetIterator for " + target.getClass().getName(), null);
/*     */     }
/*     */     
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/* 133 */     iter.next();
/* 134 */     return iter;
/*     */   }
/*     */   
/*     */   public static class AccountTargetIterator extends TargetIterator {
/*     */     private final boolean mExpandGroups;
/* 139 */     private Provisioning.GroupMembership mGroups = null;
/* 140 */     private int mIdxInGroups = 0;
/*     */     
/*     */     AccountTargetIterator(Provisioning prov, Entry target, boolean expandGroups) {
/* 143 */       super(TargetType.account, target, null);
/* 144 */       this.mExpandGroups = expandGroups;
/*     */     }
/*     */     
/*     */     Entry next() throws ServiceException
/*     */     {
/* 149 */       if (this.mNoMore) {
/* 150 */         return null;
/*     */       }
/* 152 */       Entry grantedOn = null;
/*     */       
/* 154 */       if (!this.mCheckedSelf) {
/* 155 */         if (this.mExpandGroups) {
/* 156 */           this.mCurTargetType = TargetType.dl;
/*     */         } else {
/* 158 */           this.mCurTargetType = TargetType.domain;
/*     */         }
/* 160 */         this.mCheckedSelf = true;
/* 161 */         grantedOn = this.mTarget;
/*     */       }
/* 163 */       else if (this.mCurTargetType == TargetType.dl)
/*     */       {
/*     */ 
/*     */ 
/*     */ 
/* 168 */         if (this.mGroups == null)
/*     */         {
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/* 174 */           if ((this.mTarget instanceof PseudoTarget.PseudoAccount)) {
/* 175 */             this.mGroups = new Provisioning.GroupMembership();
/*     */           }
/*     */           else {
/* 178 */             this.mGroups = this.mProv.getGroupMembership((Account)this.mTarget, false);
/*     */           }
/*     */         }
/*     */         
/* 182 */         if (this.mIdxInGroups < this.mGroups.groupIds().size())
/*     */         {
/* 184 */           grantedOn = this.mProv.getGroupBasic(Key.DistributionListBy.id, (String)this.mGroups.groupIds().get(this.mIdxInGroups));
/*     */           
/* 186 */           this.mIdxInGroups += 1;
/*     */         } else {
/* 188 */           this.mCurTargetType = TargetType.domain;
/* 189 */           grantedOn = next();
/*     */         }
/*     */       }
/* 192 */       else if (this.mCurTargetType == TargetType.domain) {
/* 193 */         this.mCurTargetType = TargetType.global;
/*     */         
/* 195 */         Domain pseudoDomain = null;
/* 196 */         if ((this.mTarget instanceof PseudoTarget.PseudoAccount)) {
/* 197 */           pseudoDomain = ((PseudoTarget.PseudoAccount)this.mTarget).getPseudoDomain();
/* 198 */         } else if ((this.mTarget instanceof PseudoTarget.PseudoCalendarResource)) {
/* 199 */           pseudoDomain = ((PseudoTarget.PseudoCalendarResource)this.mTarget).getPseudoDomain();
/*     */         }
/* 201 */         if (pseudoDomain != null) {
/* 202 */           grantedOn = next();
/*     */         } else {
/* 204 */           grantedOn = this.mProv.getDomain((Account)this.mTarget);
/*     */         }
/* 206 */       } else if (this.mCurTargetType == TargetType.global) {
/* 207 */         this.mNoMore = true;
/* 208 */         grantedOn = this.mProv.getGlobalGrant();
/*     */       }
/*     */       
/* 211 */       return grantedOn;
/*     */     }
/*     */   }
/*     */   
/*     */   public static class DistributionListTargetIterator extends TargetIterator {
/*     */     private final boolean mExpandGroups;
/* 217 */     private Provisioning.GroupMembership mGroups = null;
/* 218 */     private int mIdxInGroups = 0;
/*     */     
/*     */     DistributionListTargetIterator(Provisioning prov, Entry target, boolean expandGroups) {
/* 221 */       super(TargetType.dl, target, null);
/* 222 */       this.mExpandGroups = expandGroups;
/*     */     }
/*     */     
/*     */     Entry next() throws ServiceException
/*     */     {
/* 227 */       if (this.mNoMore) {
/* 228 */         return null;
/*     */       }
/* 230 */       Entry grantedOn = null;
/*     */       
/* 232 */       if (!this.mCheckedSelf) {
/* 233 */         if (this.mExpandGroups) {
/* 234 */           this.mCurTargetType = TargetType.dl;
/*     */         } else {
/* 236 */           this.mCurTargetType = TargetType.domain;
/*     */         }
/* 238 */         this.mCheckedSelf = true;
/* 239 */         grantedOn = this.mTarget;
/*     */       }
/* 241 */       else if (this.mCurTargetType == TargetType.dl) {
/* 242 */         if (this.mGroups == null)
/*     */         {
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/* 248 */           if ((this.mTarget instanceof PseudoTarget.PseudoDistributionList)) {
/* 249 */             this.mGroups = new Provisioning.GroupMembership();
/*     */           } else {
/* 251 */             this.mGroups = this.mProv.getGroupMembership((DistributionList)this.mTarget, false);
/*     */           }
/*     */         }
/* 254 */         if (this.mIdxInGroups < this.mGroups.groupIds().size()) {
/* 255 */           grantedOn = this.mProv.getDLBasic(Key.DistributionListBy.id, (String)this.mGroups.groupIds().get(this.mIdxInGroups));
/* 256 */           this.mIdxInGroups += 1;
/*     */         } else {
/* 258 */           this.mCurTargetType = TargetType.domain;
/* 259 */           grantedOn = next();
/*     */         }
/*     */       }
/* 262 */       else if (this.mCurTargetType == TargetType.domain) {
/* 263 */         this.mCurTargetType = TargetType.global;
/*     */         
/* 265 */         Domain pseudoDomain = null;
/* 266 */         if ((this.mTarget instanceof PseudoTarget.PseudoDistributionList)) {
/* 267 */           pseudoDomain = ((PseudoTarget.PseudoDistributionList)this.mTarget).getPseudoDomain();
/*     */         }
/* 269 */         if (pseudoDomain != null) {
/* 270 */           grantedOn = next();
/*     */         } else {
/* 272 */           grantedOn = this.mProv.getDomain((DistributionList)this.mTarget);
/*     */         }
/* 274 */       } else if (this.mCurTargetType == TargetType.global) {
/* 275 */         this.mNoMore = true;
/* 276 */         grantedOn = this.mProv.getGlobalGrant();
/*     */       }
/*     */       
/* 279 */       return grantedOn;
/*     */     }
/*     */   }
/*     */   
/*     */   public static class DynamicGroupTargetIterator extends TargetIterator {
/* 284 */     private final Provisioning.GroupMembership mGroups = null;
/* 285 */     private final int mIdxInGroups = 0;
/*     */     
/*     */     DynamicGroupTargetIterator(Provisioning prov, Entry target) {
/* 288 */       super(TargetType.group, target, null);
/*     */     }
/*     */     
/*     */     Entry next() throws ServiceException
/*     */     {
/* 293 */       if (this.mNoMore) {
/* 294 */         return null;
/*     */       }
/* 296 */       Entry grantedOn = null;
/*     */       
/* 298 */       if (!this.mCheckedSelf) {
/* 299 */         this.mCurTargetType = TargetType.domain;
/*     */         
/* 301 */         this.mCheckedSelf = true;
/* 302 */         grantedOn = this.mTarget;
/*     */       }
/* 304 */       else if (this.mCurTargetType == TargetType.domain) {
/* 305 */         this.mCurTargetType = TargetType.global;
/*     */         
/* 307 */         Domain pseudoDomain = null;
/* 308 */         if ((this.mTarget instanceof PseudoTarget.PseudoDynamicGroup)) {
/* 309 */           pseudoDomain = ((PseudoTarget.PseudoDynamicGroup)this.mTarget).getPseudoDomain();
/*     */         }
/* 311 */         if (pseudoDomain != null) {
/* 312 */           grantedOn = next();
/*     */         } else {
/* 314 */           grantedOn = this.mProv.getDomain((DynamicGroup)this.mTarget);
/*     */         }
/* 316 */       } else if (this.mCurTargetType == TargetType.global) {
/* 317 */         this.mNoMore = true;
/* 318 */         grantedOn = this.mProv.getGlobalGrant();
/*     */       }
/*     */       
/* 321 */       return grantedOn;
/*     */     }
/*     */   }
/*     */   
/*     */   public static class ConfigTargetIterator extends TargetIterator
/*     */   {
/*     */     ConfigTargetIterator(Provisioning prov, Entry target) {
/* 328 */       super(TargetType.config, target, null);
/*     */     }
/*     */   }
/*     */   
/*     */   public static class CosTargetIterator extends TargetIterator
/*     */   {
/*     */     CosTargetIterator(Provisioning prov, Entry target) {
/* 335 */       super(TargetType.cos, target, null);
/*     */     }
/*     */   }
/*     */   
/*     */   public static class DomainTargetIterator extends TargetIterator {
/*     */     private int mCurSuperDomain;
/*     */     private final List<Domain> mSuperDomains;
/*     */     
/*     */     DomainTargetIterator(Provisioning prov, Entry target) throws ServiceException {
/* 344 */       super(TargetType.domain, target, null);
/*     */       
/* 346 */       this.mCurSuperDomain = 0;
/* 347 */       this.mSuperDomains = new ArrayList();
/*     */       
/* 349 */       Domain domain = (Domain)target;
/* 350 */       String domainName = domain.getName();
/*     */       
/* 352 */       int nextDot = 0;
/* 353 */       while ((nextDot = domainName.indexOf('.')) != -1) {
/* 354 */         domainName = domainName.substring(nextDot + 1);
/* 355 */         Domain parentDomain = this.mProv.getDomain(Key.DomainBy.name, domainName, true);
/* 356 */         if (parentDomain != null) {
/* 357 */           this.mSuperDomains.add(parentDomain);
/*     */         }
/*     */       }
/*     */     }
/*     */     
/*     */     Entry next() throws ServiceException {
/* 363 */       if (this.mNoMore) {
/* 364 */         return null;
/*     */       }
/* 366 */       Entry grantedOn = null;
/*     */       
/* 368 */       if (!this.mCheckedSelf) {
/* 369 */         this.mCheckedSelf = true;
/* 370 */         grantedOn = this.mTarget;
/*     */       }
/* 372 */       else if (this.mCurSuperDomain < this.mSuperDomains.size()) {
/* 373 */         grantedOn = (Entry)this.mSuperDomains.get(this.mCurSuperDomain);
/* 374 */         this.mCurSuperDomain += 1;
/*     */       } else {
/* 376 */         this.mNoMore = true;
/* 377 */         grantedOn = this.mProv.getGlobalGrant();
/*     */       }
/*     */       
/*     */ 
/* 381 */       return grantedOn;
/*     */     }
/*     */   }
/*     */   
/*     */   public static class ServerTargetIterator extends TargetIterator
/*     */   {
/*     */     ServerTargetIterator(Provisioning prov, Entry target) {
/* 388 */       super(TargetType.server, target, null);
/*     */     }
/*     */   }
/*     */   
/*     */   public static class AlwaysOnClusterTargetIterator extends TargetIterator
/*     */   {
/*     */     AlwaysOnClusterTargetIterator(Provisioning prov, Entry target) {
/* 395 */       super(TargetType.alwaysoncluster, target, null);
/*     */     }
/*     */   }
/*     */   
/*     */   public static class UCServiceTargetIterator extends TargetIterator
/*     */   {
/*     */     UCServiceTargetIterator(Provisioning prov, Entry target) {
/* 402 */       super(TargetType.ucservice, target, null);
/*     */     }
/*     */   }
/*     */   
/*     */   public static class XMPPComponentTargetIterator extends TargetIterator
/*     */   {
/*     */     XMPPComponentTargetIterator(Provisioning prov, Entry target) {
/* 409 */       super(TargetType.xmppcomponent, target, null);
/*     */     }
/*     */   }
/*     */   
/*     */   public static class ZimletTargetIterator extends TargetIterator
/*     */   {
/*     */     ZimletTargetIterator(Provisioning prov, Entry target) {
/* 416 */       super(TargetType.zimlet, target, null);
/*     */     }
/*     */   }
/*     */   
/*     */   public static class GlobalGrantTargetIterator extends TargetIterator
/*     */   {
/*     */     GlobalGrantTargetIterator(Provisioning prov, Entry target) {
/* 423 */       super(TargetType.global, target, null);
/*     */     }
/*     */     
/*     */     Entry next() throws ServiceException
/*     */     {
/* 428 */       if (this.mNoMore) {
/* 429 */         return null;
/*     */       }
/* 431 */       Entry grantedOn = null;
/*     */       
/* 433 */       if (!this.mCheckedSelf) {
/* 434 */         this.mCheckedSelf = true;
/* 435 */         this.mNoMore = true;
/* 436 */         grantedOn = this.mTarget;
/*     */       }
/*     */       
/* 439 */       return grantedOn;
/*     */     }
/*     */   }
/*     */   
/*     */   public static void main(String[] args) throws Exception {
/* 444 */     Provisioning prov = Provisioning.getInstance();
/*     */     
/*     */ 
/*     */ 
/* 448 */     Domain sub1 = prov.createDomain("sub1.sub2.sub3.sub4.sub5.top", new HashMap());
/* 449 */     Domain sub3 = prov.createDomain("sub3.sub4.sub5.top", new HashMap());
/* 450 */     Domain sub5 = prov.createDomain("sub5.top", new HashMap());
/*     */     
/*     */ 
/*     */ 
/*     */ 
/* 455 */     System.out.println("Testing " + sub1.getName());
/* 456 */     TargetIterator targetIter = getTargetIeterator(prov, sub1, false);
/* 457 */     Entry grantedOn; while ((grantedOn = targetIter.next()) != null) {
/* 458 */       System.out.println(grantedOn.getLabel());
/*     */     }
/*     */     
/* 461 */     System.out.println();
/*     */     
/* 463 */     System.out.println("Testing " + sub3.getName());
/* 464 */     targetIter = getTargetIeterator(prov, sub3, false);
/* 465 */     while ((grantedOn = targetIter.next()) != null) {
/* 466 */       System.out.println(grantedOn.getLabel());
/*     */     }
/*     */     
/* 469 */     System.out.println();
/*     */     
/* 471 */     System.out.println("Testing " + sub5.getName());
/* 472 */     targetIter = getTargetIeterator(prov, sub5, false);
/* 473 */     while ((grantedOn = targetIter.next()) != null) {
/* 474 */       System.out.println(grantedOn.getLabel());
/*     */     }
/*     */   }
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/cs/account/accesscontrol/TargetIterator.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */